﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ifunction.LifeVC.CheckInService;


namespace ifunction.LifeVC.SingleUserCheckIn
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The session
        /// </summary>
        LifeVCSession session = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btn_Login control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            DisableControl(false);

            session = new LifeVCSession(new LifeVCAccount { Email = text_Email.Text, CellPhone = text_CellPhone.Text });

            if (!session.Login())
            {
                DisableControl(true);
            }
            else
            {
                AppendMessage("已登陆.");

                Thread thread = new Thread(new ThreadStart(MonitorDelegate));
                thread.IsBackground = true;
                thread.Start();
            }
        }

        /// <summary>
        /// Monitors the delegate.
        /// </summary>
        private void MonitorDelegate()
        {
            while (true)
            {
                try
                {
                    if (session.HasLogin)
                    {
                        if (session.LastCheckInStamp == null || session.LastCheckInStamp.Value.Date != DateTime.Now.Date)
                        {
                            AppendMessageWithinDispatcher("准备签到...");
                            string outMessage;
                            session.CheckIn(out outMessage);

                            AppendMessageWithinDispatcher(outMessage);
                        }
                        AppendMessageWithinDispatcher("维持登陆并获得状态...");
                        session.CheckSignIn();
                        AppendMessageWithinDispatcher("是否登陆: " + (session.HasLogin ? "是" : "否") + ", 积分: " + session.Points.ToString());

                    }
                    else
                    {
                        AppendMessageWithinDispatcher("准备重新登陆...");
                        session.Login();
                    }

                    Thread.Sleep(30000);
                }
                catch { }
            }
        }

        /// <summary>
        /// Disables the control.
        /// </summary>
        /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
        private void DisableControl(bool isEnabled)
        {
            text_CellPhone.IsEnabled = isEnabled;
            text_Email.IsEnabled = isEnabled;
        }

        /// <summary>
        /// Appends the message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void AppendMessage(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                text_Result.AppendText(Environment.NewLine);
            }
            else
            {
                text_Result.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + message + Environment.NewLine);
            }
        }

        /// <summary>
        /// Appends the message within dispatcher.
        /// </summary>
        /// <param name="message">The message.</param>
        private void AppendMessageWithinDispatcher(string message)
        {
            text_Result.Dispatcher.Invoke(new Action(() =>
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    this.text_Result.AppendText(Environment.NewLine);
                }
                else
                {
                    this.text_Result.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + message + Environment.NewLine);
                }
            }));
        }
    }
}
